package jumpInstructions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import main.MIPSSimulator;

public class Jalr extends MIPSJumpInstruction {
	public Jalr(String input) throws FileNotFoundException, IOException {
		myRecognizedRegisters.load(new FileInputStream(new File(
				"recognizedRegisters.properties")));
		myRegisterOne = myRecognizedRegisters.getProperty(input
				.substring(6, 11));
		myRegisterTwo = myRecognizedRegisters.getProperty(input
				.substring(16, 21));
		myName = "jalr";
	}
	public String toString(){
		return myName + " " + myRegisterOne + ", " + myRegisterTwo;
	}
	@Override
	public void execute() {
		MIPSSimulator.myRegisters.put(myRegisterTwo, MIPSSimulator.PC + 1);
		if (myRegisterOne.equals("$ra")) {
			MIPSSimulator.PC = (MIPSSimulator.PC & 0xF0000000) | (MIPSSimulator.myRegisters.get(myRegisterOne) & 0xFFFF);
		} else
			MIPSSimulator.PC = (MIPSSimulator.PC & 0xF0000000) | ((MIPSSimulator.myRegisters.get(myRegisterOne) / 4) & 0xFFFF);
	}
}
